package com.house365.core.adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.WeakHashMap;

import android.content.Context;
import android.graphics.Color;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Gallery;

import com.house365.core.R;
import com.house365.core.application.BaseApplication;
import com.house365.core.constant.CorePreferences;
import com.house365.core.image.AsyncImageLoader;
import com.house365.core.image.CacheImageUtil;
import com.house365.core.touch.ImageViewTouch;

/**
 * @author py
 * @date 2012-8-10
 * @comment
 */
public class BaseAlbumAdapter {
	protected final List<String> list;
	private Context context;
	protected BaseApplication application;
	protected AsyncImageLoader mAil;
	private AlubmPagerAdapter pageAdapter;
	private GalleryAdapter galleryAdapter;
	private int img_def_big = R.drawable.img_default_big;
	private int resBg;//默认背景
    
    /** 起始最大缩放比例. */
    private float mInitialMaxZoom = 3.0f;
	
	public AlubmPagerAdapter getPageAdapter() {
		return pageAdapter;
	}

	public void setPageAdapter(AlubmPagerAdapter pageAdapter) {
		this.pageAdapter = pageAdapter;
	}

	public int getResBg() {
		return resBg;
	}

	public void setResBg(int resBg) {
		this.resBg = resBg;
	}

	public GalleryAdapter getGalleryAdapter() {
		return galleryAdapter;
	}

	public void setGalleryAdapter(GalleryAdapter galleryAdapter) {
		this.galleryAdapter = galleryAdapter;
	}
	public void setGallery(Gallery gallery){
		this.galleryAdapter.setGallery(gallery);
	}
	public BaseAlbumAdapter(Context context) {
		this.context = context;
		this.list = new ArrayList<String>();
		application = (BaseApplication) context.getApplicationContext();
		mAil = new AsyncImageLoader(context);
		pageAdapter = new AlubmPagerAdapter();
		galleryAdapter = new GalleryAdapter(context);
	}

	public boolean addAll(List<String> list) {
		this.galleryAdapter.addAll(list);
		return this.list.addAll(list);
	}

	public void clear() {
		this.galleryAdapter.clear();
		//TODO
//		pageAdapter.destroyItem(container, position, object)
		this.list.clear();
	}

	public int getCount() {
		return list == null ? 0 : list.size();
	}

	public String getItem(int i) {
		return this.list.get(i);
	}

	public void notifyDataSetChanged(){
		this.galleryAdapter.notifyDataSetChanged();
		this.pageAdapter.notifyDataSetChanged();
	}
	public void setTouchImage(final ImageViewTouch imageView, String imageUrl,
			final int resId, int scaleType) {
		CacheImageUtil.setTouchImage(imageView, imageUrl,
				context.getResources(), resId, scaleType, mAil);
	}
	public ImageViewTouch getImageView(int position) {
//		ImageViewTouch view = pageAdapter.views.get(position).get();;
		
		return pageAdapter.views.get(position).get();
	}
	
	
	public void setImg_def_big(int img_def_big) {
		this.img_def_big = img_def_big;
	}

    /** 
     * 设置起始最大缩放比例.
     */
    public void setInitialMaxZoom(float initialMaxZoom) {
        mInitialMaxZoom = initialMaxZoom;
    }


	class AlubmPagerAdapter extends PagerAdapter {
		public HashMap<Integer, SoftReference<ImageViewTouch>> views = new HashMap<Integer, SoftReference<ImageViewTouch>>();

		@Override
		public Object instantiateItem(View container, int position) {
			ImageViewTouch imageView = new ImageViewTouch(context);
			imageView.setLayoutParams(new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			imageView.setInitialMaxZoom(mInitialMaxZoom);
			if(resBg != 0){
				setTouchImage(imageView,"",
						resBg,AsyncImageLoader.IMAGE_SCALE_TYPE_MIDDLE);
			}else{
				imageView.setBackgroundColor(Color.BLACK);
			}
//			imageView.setFocusableInTouchMode(true);
			String url = getItem(position);
			setTouchImage(imageView,url,
					img_def_big,
					AsyncImageLoader.IMAGE_SCALE_TYPE_HIGH);
			((ViewPager) container).addView(imageView);
			views.put(position, new SoftReference<ImageViewTouch>(imageView));
			return imageView;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			views.remove(position);
			ImageViewTouch imageView = (ImageViewTouch) object;
			imageView.mBitmapDisplayed.recycle();
			imageView.clear();
			((ViewPager) container).removeView(imageView);
		
		}

		@Override
		public void startUpdate(View container) {
		}

		@Override
		public void finishUpdate(View container) {
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == ((ImageViewTouch) object);
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
		}

		@Override
		public int getCount() {
			return BaseAlbumAdapter.this.getCount();
		}
	}
}
